﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Architecture.CrossCutting.Tools.Logging;
using System.Threading;

namespace Architecture.Presentation.ApplicationConsole
{



    class Program
    {
        static void Main(string[] args)
        {




            //Task[] taskArray = new Task[10];
            //for (int i = 0; i < taskArray.Length; i++)
            //{
            //    taskArray[i] = Task.Factory.StartNew((Object obj) =>
            //    {
            //        CustomData data = obj as CustomData;
            //        if (data == null)
            //            return;

            //        using (UnitOfWork ctx = new UnitOfWork())
            //        {
            //            ctx.LogRepository.Insert(new Log()
            //            {
            //                Message = String.Format("Log : {0}", data.Name)
            //            });

            //            ctx.Save();
            //        }

            //        data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
            //    },
            //    new CustomData() { Name = i + 10, CreationTime = DateTime.Now.Ticks });
            //}
            //Task.WaitAll(taskArray);
            //foreach (var task in taskArray)
            //{
            //    var data = task.AsyncState as CustomData;
            //    if (data != null)
            //        Console.WriteLine("Task #{0} created at {1}, ran on thread #{2}.",
            //                          data.Name, data.CreationTime, data.ThreadNum);
            //}


            Task.Factory.StartNew(() =>
            {
                CancellationToken token = new CancellationToken();
                using (UnitOfWork ctx = new UnitOfWork())
                {
                    var results = ctx.LogRepository.GetAsync(p => p.OrderBy(r => r.LogId),token);

                    foreach (var r in results.Result)
                    {
                        System.Console.WriteLine(r.Message);
                    }

                }
            });


                System.Console.ReadLine();


        }

        private class CustomData
        {
            public CustomData()
            {
            }

            public long CreationTime { get; set; }
            public int Name { get; set; }
            public int ThreadNum { get; set; }
        }
    }
}
